package com.judysen.jsqlweb.services;

import com.judysen.jsqlweb.aop.JsqlLoggerAnnotation;
import com.judysen.jsqlweb.exceptions.ServiceException;
import com.judysen.jsqlweb.models.DatabaseConfigModel;
import com.judysen.jsqlweb.mybatis.mapper.DatabaseConfigMapper;
import com.judysen.jsqlweb.utils.JsqlDynamicDatasource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service("databaseConfigService")
public class DatabaseConfigService {
    @Autowired
    private JsqlDynamicDatasource dynamicDataSource;
    @Autowired
    private DatabaseConfigMapper configMapper;

    /**
     * 保存一个数据库连接
     *
     * @param model
     * @return
     */
    @JsqlLoggerAnnotation
    public int save(DatabaseConfigModel model) {
        var flag = 0;
        var config = configMapper.selectOneByDisplayName(model.getDisplayName());
        if (config == null) {
            flag = configMapper.save(model);
        } else {
            throw new ServiceException(400, "已经存在这个数据库显示名了，请确认是否新增，或者是修改数据库链接。");
        }
        //---将连接加入到DynamicDataSource中

        dynamicDataSource.setDatabaseByConfig(model);

        return flag;
    }

    /**
     * @param updateModel
     * @return
     */
    @JsqlLoggerAnnotation
    public int update(DatabaseConfigModel updateModel) {
        var config = configMapper.selectOne(updateModel.getId());
        if (config == null) {
            throw new ServiceException(400, "找不到对应的数据库连接。");
        }
        var flag = configMapper.update(updateModel);
        return flag;
    }

    /**
     * 删除数据库配置
     *
     * @param id
     * @return
     */
    @JsqlLoggerAnnotation
    public int delete(Integer id) {
        var config = configMapper.selectOne(id);
        var flag = 0;
        if (config != null) {
            flag = configMapper.delete(id);
            dynamicDataSource.removeDatabase(config.getDisplayName());
        }
        return flag;
    }

    /**
     * 系统启动的时候，初始化所有的数据库里的数据信息
     */
    @PostConstruct
    public void initDatasource() {
        var configs = configMapper.getAllDatabaseConfig();
        dynamicDataSource.setAllDatabase(configs);
    }

    @JsqlLoggerAnnotation
    public List<DatabaseConfigModel> getAllConfigs() {
        var configs = configMapper.getAllDatabaseConfig();
        return configs;
    }

    /**
     * 通过id获取
     *
     * @param id
     * @return
     */
    @JsqlLoggerAnnotation
    public DatabaseConfigModel getDatabaseConfig(Integer id) {
        var config = configMapper.selectOne(id);
        return config;
    }

    /**
     * 通过显示名获取配置
     *
     * @param displayName
     * @return
     */
    @JsqlLoggerAnnotation
    public DatabaseConfigModel getDatabaseConfigByDisplayName(String displayName) {
        var config = configMapper.selectOneByDisplayName(displayName);
        return config;
    }

    /**
     * 获取所有的显示名
     * @return
     */
    @JsqlLoggerAnnotation
    public List<String> getAllDisplayName(){
        return configMapper.selectAllDisplayName();
    }
}